package ldh.im.client;

import ldh.im.client.util.RsaUtil;
import org.apache.commons.codec.binary.Base64;
import org.junit.Test;

import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Map;

public class RsaDemo {

    @Test
    public void test() throws Exception {
        Map<String, Object> result =  RsaUtil.initKey();
        RSAPublicKey publicKey = (RSAPublicKey) result.get("RSAPublicKey");
        RSAPrivateKey privateKey = (RSAPrivateKey) result.get("RSAPrivateKey");
        System.out.println("公钥：" + new String(Base64.encodeBase64(publicKey.getEncoded())));
        System.out.println("私钥：" + new String(Base64.encodeBase64(privateKey.getEncoded())));

        // KeyPairGenerator类用于生成公钥和私钥对，基于RSA算法生成对象
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
        // 初始化密钥对生成器，密钥大小为96-1024位
        keyPairGen.initialize(1024,new SecureRandom());
        // 生成一个密钥对，保存在keyPair中
        KeyPair keyPair = keyPairGen.generateKeyPair();
        RSAPrivateKey privateKey2 = (RSAPrivateKey) keyPair.getPrivate();   // 得到私钥
        RSAPublicKey publicKey2 = (RSAPublicKey) keyPair.getPublic();  // 得到公钥
        String publicKeyString = new String(Base64.encodeBase64(publicKey2.getEncoded()));
        // 得到私钥字符串
        String privateKeyString = new String(Base64.encodeBase64((privateKey2.getEncoded())));
        System.out.println("公钥：" + publicKeyString);
        System.out.println("私钥：" + privateKeyString);
    }

    @Test
    public void encode() throws Exception {
        String encodeJson = "VMIEobCobWPT8A5h60ZvB4UNNAGYiojfxnbog4NjTNudHCfM2+3AhpnPOIqkqc2Dlnne3d591M2sTCRTu/j9HusfTxcIPw0Jd40RmB2U6AEFKww65Jc0phoamKcj0gKY+V" +
                "/dtSoBy5jLcaAAWF0mOLKhi1nkTP36FPRsZ6VW7C4=";

        String pvk="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIncDEXGFyhCrS/efbpAOq04uqro/SC0lcaKYmgox9TCEC4W67ghZONK02hxFA2mAq0wLOf4mhtDuQGRix025JytD/UTFW3biCGy/thmE1DnPZI0hTPg1io/zaY4GXpAFk/KyUKClEY1vpCgjuR1/pdF/jQRXYI6V6iaFWDoUW6dAgMBAAECgYBMhtZZNSlNtBWnaP6qEVWBKuiUT1mUeipaWB9gaqLjPAb3TsnzILZ8ZeENLRhQq0L5GLEU5C/HysQ8Vqq+wOYOMmd6NAVE/4bwc/1WJr0+ydPFWt3TRTRZ71aFXwd5Y+M2/+yDEoD2ES5uyS0ECn8Pi7svtnIjTg/+h9KHlKt3AQJBAMHY87fClwzq5izZ21AwEGlWBxjIy+zPfYDtLqDuqJpI4snENUOkJ29F+yp/c2mRMR95hKYPIxoVzsy89mP8ATECQQC2D5hCFEKpfQEDYzUR7FUvxxEvBcyda10lqu0J1WOKL4bvCXpaEl8a4aFB9gQX+VyBIixo9PadEUOqYQFWAoktAkBWzzaNs4n7kfJUYZeyeGPRGGKDOeEBAwacRxtbnKVKups9ai3ERe4XCcrqBDuGKixKgRvjVeINW3jJ/VGGAyPxAkAL4DqLXOz4R1owkWEd2S+5XR/m1FcDkcRSt7KcqJnEotMHdrPPelFP3JX7SoP72F1LYyJIemo31q92Rruik6nFAkEAr+LD6pToIXUn19C1wPaWJ7+dSuklq2TRMIeN+qCvxSwuWfoapBWuu5IOLVYHuULlm6L9vH8XRtjHIuwdVeAa7Q==";
        String pk = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCJ3AxFxhcoQq0v3n26QDqtOLqq6P0gtJXGimJoKMfUwhAuFuu4IWTjStNocRQNpgKtMCzn+JobQ7kBkYsdNuScrQ" +
                "/1ExVt24ghsv7YZhNQ5z2SNIUz4NYqP82mOBl6QBZPyslCgpRGNb6QoI7kdf6XRf40EV2COleomhVg6FFunQIDAQAB";
        String data = "dsfafdasfasfa21423asdfasfdasfdasfa";
        String ed = RsaUtil.encryptByPrivateKey(data, pvk);
        String decodeJson = RsaUtil.decryptByPublicKey(encodeJson, pk);
        System.out.println("data:" + decodeJson);
    }
}
